/*

  The MIT License

  Copyright (c) 2008 - 2011. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/
package
{

import com.bit101.components.PushButton;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.registerClassAlias;
import flash.sampler.getSize;
import flash.utils.ByteArray;
import flash.utils.describeType;
import flash.utils.getTimer;

import ru.goodcore.composite.Branch;
import ru.goodcore.composite.Leaf;
import ru.goodcore.core.GoodCore;
import ru.goodcore.display.ui.GraffitiContainer;
import ru.goodcore.display.ui.core.Application;
import ru.goodcore.display.ui.core.Container;
import ru.goodcore.display.ui.core.UIComponent;
import ru.goodcore.logging.Log;
import ru.goodcore.logging.targets.BuiltInDebugTarget;
import ru.goodcore.logging.targets.ILoggingTarget;
import ru.goodcore.logging.targets.TraceTarget;
import ru.goodcore.net.registerClass;
import ru.goodcore.utils.ActivityListener;
import ru.goodcore.utils.ObjectUtil;
import ru.goodcore.utils.StringUtils;
import ru.goodcore.utils.traceByteArray;

/**
 *  @author				Dimarik
 *  @version			1.1
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
[SWF(width="800", height="600", frameRate="21", backgroundColor="#E0EEEE")]

public class InvalidationTest extends GoodCore {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor
	 */
	public function InvalidationTest() {
		CONFIG::logging {
			const traceTarget:ILoggingTarget = new TraceTarget();
			traceTarget.formatter.includeMeasuredTime = true;
			Log.addTarget(traceTarget);
		}

		super();
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------
	/**
	 * @private
	 */
	CONFIG::logging
	private const _target:		BuiltInDebugTarget = new BuiltInDebugTarget();

	/**
	 * @private
	 */
	private var _parentUI:		GraffitiContainer;// = new GraffitiContainer();

	private var _childUI:		GraffitiContainer;// = new GraffitiContainer();

	private var _childUI_2:		GraffitiContainer;// = new GraffitiContainer();

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------
	/**
	 * @private 
	 */
	protected override function creationComplete():void {
		super.stage.scaleMode = StageScaleMode.NO_SCALE;
		super.stage.align = StageAlign.TOP_LEFT;

		super.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.handler_mouseMonitor);
		super.stage.addEventListener(Event.MOUSE_LEAVE, this.handler_mouseMonitor);

		var activityListener:ActivityListener = new ActivityListener();
		activityListener.width = 150;
		activityListener.x = 5;
		activityListener.y = 5;
		super.stage.addChild(activityListener);
	

		// UI
		var button:PushButton;;
		button = new PushButton(super.stage, 10, 200, '+', this.handler_pressPlusButton);
		button = new PushButton(super.stage, 10, 225, '-', this.handler_pressMinusButton);
		button = new PushButton(super.stage, 700, 200, '+', this.handler_pressPlus1Button);
		button = new PushButton(super.stage, 700, 225, '-', this.handler_pressMinus1Button);
		button = new PushButton(super.stage, 10, 250, 'color', this.handler_pressColorButton);
		button = new PushButton(super.stage, 10, 275, 'clip', this.handler_pressClipButton);
		button = new PushButton(super.stage, 10, 300, 'add', this.handler_pressAddButton);
		button = new PushButton(super.stage, 10, 325, 'remove', this.handler_pressRemoveButton);

		super.style = {
			backgroundImage: 1
		};

		super.percentWidth = 100;
		super.percentHeight = 100;

		var numContainers:uint = 1;
		var parent:	DisplayObjectContainer = this;
		var app:	Container;

		for (var i:uint = 0; i < numContainers; i++) {
			app = new Container();
			app.percentWidth = 99;//i / numContainers * 100 + 1;
			app.percentHeight = 99;//i / numContainers * 100 + 1;
			//app.height = 90;//i / numContainers * 100 + 1;
			parent = parent.addChild(app) as DisplayObjectContainer;
		}
	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Event handlers
	//
	//-------------------------------------------------------------------------

	private function handler_enterFrame(event:Event):void {
		trace('handler_enterFrame');
		super.removeEventListener(event.type, arguments.callee);
		//super.addEventListener(Event.ENTER_FRAME, this.handler_enterFrame2);
		const parentUI:	GraffitiContainer = this._parentUI;
		const childUI:	GraffitiContainer = this._childUI;
		const childUI2:	GraffitiContainer = this._childUI_2;

		childUI.lineColor = 0xC08000;
		var style:Object = {right:20, bottom:20, paddingTop:10, paddingBottom:10, paddingLeft:10, paddingRight:10};
		childUI.style = style;
		childUI.width = 150;
		childUI.height = 150;

		childUI2.width = 100;
		childUI2.height = 100;
		style = {top:10, left:10};
		childUI2.style = style;
	}

	private function handler_enterFrame2(event:Event):void {
		super.removeEventListener(event.type, arguments.callee);
		//super.addEventListener(Event.ENTER_FRAME, this.handler_enterFrame3);
		const parentUI:	GraffitiContainer = this._parentUI;
		const childUI:	GraffitiContainer = this._childUI;
		const childUI2:	GraffitiContainer = this._childUI_2;

		trace('handler_enterFrame2');

		childUI2.lineColor = 0x00FFFF;
		childUI2.width = 100;
		childUI2.height = 100;
		var style:Object = {top:10, left:10};
		childUI2.style = style;

	}

	private function handler_enterFrame3(event:Event):void {
		super.removeEventListener(event.type, arguments.callee);
		const parentUI:	GraffitiContainer = this._parentUI;
		const childUI:	GraffitiContainer = this._childUI;
		const childUI2:	GraffitiContainer = this._childUI_2;

		trace('handler_enterFrame3', childUI2.parent ? 'remove' : 'add');
		if (childUI2.parent)
			childUI.removeChild(childUI2);
		else
			childUI.addChild(childUI2);
	}

	/**
	 * @private
	 */
	private function handler_pressPlusButton(event:Event):void {
		this._parentUI.width += 10;
		this._parentUI.height += 10;
	}

	/**
	 * @private
	 */
	private function handler_pressMinusButton(event:Event):void {
		this._parentUI.width -= 10;
		this._parentUI.height -= 10;
	}

	/**
	 * @private
	 */
	private function handler_pressPlus1Button(event:Event):void {
		this._childUI.width += 10;
		this._childUI.height += 10;
	}

	/**
	 * @private
	 */
	private function handler_pressMinus1Button(event:Event):void {
		this._childUI.width -= 10;
		this._childUI.height -= 10;
	}

	/**
	 * @private
	 */
	private function handler_pressColorButton(event:Event):void {
		this._parentUI.bgColor = uint( Math.random() * 0xFFFFFF );
	}

	/**
	 * @private
	 */
	private function handler_pressClipButton(event:Event):void {
		this._parentUI.clipContent = !this._parentUI.clipContent;
	}

	private var _stage:		Stage;

	/**
	 * @private
	 */
	private function handler_pressAddButton(event:Event):void {
		if (super.stage)
			return;

		this._stage.addChild(this);
	}

	/**
	 * @private
	 */
	private function handler_pressRemoveButton(event:Event):void {
		if ( !super.stage )
			return;

		this._stage = super.stage;
		super.stage.removeChild(this);
	}

	/**
	 * @private
	 */
	private function handler_click(event:Event):void {
		var component:GraffitiContainer = event.target as GraffitiContainer;

		component.parent.removeChild(component);

		super.addChild(component);

		if (component) {
			component.width = int(Math.random() * 100) + 100;
			component.height = int(Math.random() * 100) + 100;
			component.bgColor = uint( Math.random() * 0xFFFFFF );
		}

	}

	private function handler_mouseMonitor(event:Event):void {
		trace('handler_mouseMonitor', event);
	}

}
}